//
//  QuotesBottomCell.m
//  CBT
//
//  Created by 四点钟 on 2018/11/17.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "QuotesBottomCell.h"

@interface QuotesBottomCell()

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *rootView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *moreBtn;

@end

@implementation QuotesBottomCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle   = UITableViewCellSelectionStyleNone;
        self.backgroundColor  = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self initSubViews];
    }
    return self;
}

-(void)setModel:(QuotesModel *)model
{
    _model = model;
    [_moreBtn setTitle:kLocalizedString(@"查看全文", nil) forState:(UIControlStateNormal)];
    _titleLabel.text  = model.titleLabelText;
    _detailLabel.text = model.detailLabelText;
    _timeLabel.text   = model.timeLabelText;
    [_moreBtn setImage:[UIImage imageNamed:model.isOpen ? @"hangqing_shangla" : @"hangqing_xiala"] forState:(UIControlStateNormal)];
    
    __weak typeof(self) weakSelf = self;
    [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(12);
        make.height.mas_offset(weakSelf.model.detailLabelHeight);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(14);
        make.height.mas_offset(weakSelf.model.titleLabelHeight);
    }];
}

- (void)initSubViews
{
    _rootView = [[UIView alloc]init];
    _rootView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_rootView];
    
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = LineViewBgColor;
    [_rootView addSubview:_lineView];
    
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    _titleLabel.numberOfLines = 0;
    _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _titleLabel.textColor = UIColorFromRGB(0x333333);
    [_rootView addSubview:_titleLabel];
    
    _detailLabel = [[UILabel alloc]init];
    _detailLabel.font = [UIFont systemFontOfSize:14];
    _detailLabel.numberOfLines = 0;
    _detailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _detailLabel.textColor = UIColorFromRGB(0x666666);
    [_rootView addSubview:_detailLabel];
    
    _moreBtn = [[UIButton alloc]init];
    _moreBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_moreBtn setTitleColor:DefaultBlueColor forState:(UIControlStateNormal)];
    _moreBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    _moreBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 65, 0, 0);
    [_moreBtn addTarget:self action:@selector(moreBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_rootView addSubview:_moreBtn];
    
    _timeLabel = [[UILabel alloc]init];
    _timeLabel.font = [UIFont systemFontOfSize:12];
    _timeLabel.textColor = UIColorFromRGB(0x666666);
    [_rootView addSubview:_timeLabel];
}

- (void)moreBtnAction:(UIButton *)btn
{
    if (self.cellOPenBlock != nil) {
        self.cellOPenBlock(self.indexRow);
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
     __weak typeof(self) weakSelf = self;
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [_rootView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(14);
        make.height.mas_offset(weakSelf.model.titleLabelHeight);
    }];
    
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(12);
        make.height.mas_offset(weakSelf.model.detailLabelHeight);
    }];
    
    [_moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.detailLabel.mas_bottom).offset(14);
        make.height.mas_lessThanOrEqualTo(22);
        make.width.mas_equalTo(90);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.detailLabel.mas_bottom).offset(14);
        make.height.mas_equalTo(12);
        make.right.lessThanOrEqualTo(self.moreBtn.mas_left).offset(15);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
